package com.shenganyuan.smac.common.module.imager;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.oklib.widget.imageloader.ImageLoader;
import com.oklib.widget.imageloader.ImageLoaderUtil;
import com.shenganyuan.smac.App;
import com.shenganyuan.smac.R;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ImagePagerAdapter extends PagerAdapter {

    private ArrayList<String> images;
    private Context mContext;

    public ImagePagerAdapter(Context context, ArrayList<String> images) {
        this.images = images;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View viewLayout = LayoutInflater.from(mContext).inflate(R.layout.common_photoview_item, view, false);
        PhotoView imageView = viewLayout.findViewById(R.id.image);
        final ProgressBar spinner = viewLayout.findViewById(R.id.loading);
        ImageLoaderUtil.getInstance().loadImage(App.getInstance(), new ImageLoader.Builder().url(images.get(position)).imgView(imageView).build());
        view.addView(viewLayout, 0); // 将图片增加到ViewPager
        imageView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float v, float v1) {
                Activity activity = (Activity) mContext;
                activity.onBackPressed();
            }

            @Override
            public void onOutsidePhotoTap() {

            }
        });
        return viewLayout;
    }

    @Override
    public void finishUpdate(View container) {
    }

}